package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RestController
public class SessionController {
    @RequestMapping("create")
    //如果有存放该用户的session对象,则直接取出，否则，为用户创建一个session对象
    public String createSession(HttpServletRequest request ){
        HttpSession session = request.getSession();
        //在session中绑定共享对象，在一个会话范围内共享
        session.setAttribute("userName","tom");
        return session.getId();
    }
    @RequestMapping("getInfo")
    public String getSessionInfo(HttpServletRequest request){
        //从session中取出共享数据
        HttpSession session = request.getSession();
        Object userName = session.getAttribute("userName");
        return userName.toString();
    }
}
